/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Entidad.Tarea;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
/**
 *
 * @author Stiv
 */
public class TareaDAO {
    
    private static EntityManagerFactory emf=Persistence.createEntityManagerFactory("CRMPU");



    public boolean crear(Tarea object) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean salida=false;
        try {
            em.persist(object);
            em.getTransaction().commit();
            salida = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return salida;
        }
    }

    
    public boolean eliminar(Tarea object) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Tarea tar = em.find(Tarea.class, object.getID());
        boolean ret = false;
        try {
            em.remove(tar);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }


    public Tarea leer(Tarea par) {

        EntityManager em = emf.createEntityManager();
        Tarea tarea = em.find(Tarea.class, par.getID());
        return tarea;
    }

        
    public Tarea leer(int par) {

        EntityManager em = emf.createEntityManager();
        Tarea tarea = null;
        Query q = em.createQuery("SELECT p from Tarea p " +
                "WHERE p.ID = :id")
                .setParameter("id", par);
        try {
            tarea = (Tarea) q.getSingleResult();
        } catch (NonUniqueResultException e) {
            tarea = (Tarea) q.getResultList().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            return tarea;
        }
    }
    
  
    public boolean actualizar(Tarea object, Tarea nuevoObjeto) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean ret = false;
        try {
            object.setID(nuevoObjeto.getID());
            object.setFecha_in(nuevoObjeto.getFecha_in());
            object.setFecha_fin(nuevoObjeto.getFecha_fin());
            object.setTipo(nuevoObjeto.getTipo());
            object.setDescripcion(nuevoObjeto.getDescripcion());
            object.setEstado(nuevoObjeto.getEstado());
            object.setProgreso(nuevoObjeto.getProgreso());
            object.setCompra(nuevoObjeto.getCompra());
            object.setEmpleado(nuevoObjeto.getEmpleado());
            object.setNombre(nuevoObjeto.getNombre() );
            em.merge(object);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }

    public ArrayList<Integer> listar (){
        ArrayList<Integer> ids = new ArrayList<Integer>();

        EntityManager em = emf.createEntityManager();
        List lista = null;
        Query q = em.createQuery("SELECT c from Tarea c ");
        try {
            lista = q.getResultList();
            for (int i=0; i<lista.size(); i++){
                Tarea p = (Tarea)lista.get(i);
                ids.add(p.getID());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            return ids;
        }
    }
    
    
    //Metodo para traer todos los productos
    public List<Tarea> bringAll(){
        EntityManager em = emf.createEntityManager();
        List<Tarea> cat = new ArrayList<Tarea>();
        
        Query q= em.createQuery("SELECT u FROM Tarea u");
        List<Tarea> tempCat = q.getResultList();
        try {
        for(Tarea c: tempCat){
            Tarea cate = new Tarea();
            cate.setNombre(c.getNombre());
            cate.setID(c.getID());
            cate.setProgreso(c.getProgreso());
            cate.setFecha_in(c.getFecha_in());
            cate.setFecha_fin(c.getFecha_fin());
            cate.setTipo(c.getTipo());
            cate.setDescripcion(c.getDescripcion());
            cate.setEstado(c.getEstado());
            cate.setCompra(c.getCompra());
            cate.setEmpleado(c.getEmpleado());
            cat.add(cate);
        }
        
        }
        catch(Exception e){
            e.printStackTrace();;            
        }
        finally {
            
            em.close();
            return cat;
        }
    }
    
    
    //Metodo al que entra el nombre de la tarea y me devuelve el ID
    public Tarea leerNombre(String nombre) {

        EntityManager em = emf.createEntityManager();
        Tarea tarea = null;
        Query q = em.createQuery("SELECT p from Tarea p " +
                "WHERE p.nombre LIKE :nom")
                .setParameter("nom", nombre);
        try {
            tarea = (Tarea) q.getSingleResult();
        } catch (NonUniqueResultException e) {
            tarea = (Tarea) q.getResultList().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            return tarea;
        }
    }
    
}
